package com.ac.common.core.result;

import com.ac.common.core.result.config.ResultStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @author : 小凡
 * date : create in 2021/7/22 15:11
 * description : 返回类
 **/
@Builder
@Data
@AllArgsConstructor
@Schema(name = "Result", description = "通用返回对象")
public class Result<T> implements Serializable {

    @Schema(name = "data", description = "数据封装")
    public T data;
    @Schema(name = "code", description = "状态码")
    public int code;
    @Schema(name = "message", description = "提示信息")
    public String msg;

    public Result(T data) {
        ResultStatus resultStatus = ResultStatus.SUCCESS;
        code = resultStatus.code;
        msg = resultStatus.message;
        this.data = data;
    }

    public Result(ResultStatus resultStatus, T data) {
        code = resultStatus.code;
        msg = resultStatus.message;
        this.data = data;
    }

    public static <T> Result<T> ok(T data) {
        return new Result<T>(data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return Result.<T>builder().code(ResultStatus.SUCCESS.code).msg(msg).data(data).build();
    }

    public static <T> Result<T> ok(String msg, Integer code, T data) {
        return Result.<T>builder().code(code).msg(msg).data(data).build();
    }

    public static <T> Result<T> failure(String msg) {
        return Result.<T>builder().code(ResultStatus.FAIL.code).msg(msg).build();
    }

    public static <T> Result<T> failure() {
        return Result.<T>builder().code(ResultStatus.FAIL.code).msg(ResultStatus.FAIL.message).build();
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
